package com.itita.ww2.handler.requesthandler.worldmap;

import org.apache.commons.lang.StringUtils;

import com.itita.ww2.game.alliance.AllianceManager;
import com.itita.ww2.game.user.UserService;
import com.itita.ww2.game.worldmap.WorldMapManager;
import com.itita.ww2.game.worldmap.WorldMapService;
import com.itita.ww2.game.worldmap.event.WorldMapEventDispatch;
import com.itita.ww2.handler.requesthandler.worldmap.utils.BattleLogUtil;
import com.itita.ww2.model.user.UserProfile;
import com.itita.ww2.model.worldmap.City;
import com.itita.ww2.model.worldmap.enums.CityType;
import com.itita.ww2.puredb.model.Alliance;

/**
 * 每日结算
 * 时间结束
 * @author NorthLan
 *
 */
public class DailyReckoningTimeEnd implements Runnable {

	@Override
	public void run() {
		// 暂时先清除掉所有的宣战状态
		for (City city : WorldMapManager.getInstance().getAllCity()) {
			if (city.getType() == CityType.CAPTICAL) {
				String ownerAid = city.getOwnerAid();
				String declarAid = city.getDeclarAid();
				if (StringUtils.isBlank(ownerAid) || StringUtils.isBlank(declarAid)) {
					continue;
				}
				Alliance ownerAlliance = AllianceManager.getInstance().getAlliance(ownerAid);
				if (ownerAlliance != null) {
					UserProfile ownerProfile = UserService.getInstance().getUserProfile(ownerAlliance.getOwnerUid());
					// 战报 type = 4
					WorldMapService.addBattleLog(BattleLogUtil.log4(ownerProfile.getNickname(), ownerAlliance.getName(),
							city.getBlockId(), city.getId()));
				}
				city.setDeclarAid("");
				city.setLastDeclarTime(null);
				city.refreshOwnerState();
				city.refreshStatus();
				WorldMapEventDispatch.onCityDataUpdate(city);
			}
		}
	}

}
